#!/bin/bash

# 定义安装路径和一些常量
FISCO_DIR=~/fisco
JAVA_HOME_DIR="/usr/lib/jvm/java-8-openjdk-amd64"

# 定义加载动画函数
show_loading() {
    local pid=$1
    local message=$2
    local spin='-\|/'
    local i=0
    while kill -0 $pid 2>/dev/null; do
        i=$(( (i+1) %4 ))
        printf "\r${message} ${spin:$i:1}"
        sleep .1
    done
    printf "\r"
}

# 安装依赖包
echo "🔧 Step 1: Install dependencies..."

# 先添加 openjdk-8-jdk 的仓库
sudo add-apt-repository ppa:openjdk-r/ppa -y
sudo apt update -y

# 安装 OpenJDK 8 以及其他依赖
sudo apt install -y openjdk-8-jdk openssl curl wget vim unzip tree

if [ $? -eq 0 ]; then
    echo "✅ Dependencies installed successfully."
else
    echo "❌ Failed to install dependencies!"
    exit 1
fi

# 配置 Java 环境变量
echo "🛠 Step 2: Configure Java environment variables..."
if ! grep -q "JAVA_HOME" /etc/profile; then
    echo "export JAVA_HOME=$JAVA_HOME_DIR" | sudo tee -a /etc/profile > /dev/null
    echo "export PATH=\$JAVA_HOME/bin:\$PATH" | sudo tee -a /etc/profile > /dev/null
    source /etc/profile
    echo "✅ JAVA_HOME is set to $JAVA_HOME_DIR"
else
    echo "✅ JAVA_HOME is already configured."
fi

# 拉取 FISCO BCOS 安装脚本并启动节点
echo "⛓ Step 3: Install FISCO BCOS..."
mkdir -p $FISCO_DIR && cd $FISCO_DIR
curl -#LO https://gitee.com/FISCO-BCOS/FISCO-BCOS/releases/download/v2.11.0/build_chain.sh
chmod u+x build_chain.sh
if [ $? -eq 0 ]; then
    echo "✅ FISCO BCOS installation script downloaded."
else
    echo "❌ Failed to download FISCO BCOS installation script!"
    exit 1
fi

# 编译 FISCO BCOS 链，指定 4 个节点
echo "🚀 Step 4: Build single-group 4-node FISCO BCOS..."
bash build_chain.sh -l 127.0.0.1:4 -p 30300,20200,8545
if [ $? -eq 0 ]; then
    echo "✅ FISCO BCOS nodes built successfully."
else
    echo "❌ Failed to build FISCO BCOS nodes!"
    exit 1
fi

# 启动 FISCO BCOS 节点
echo "⚡ Step 5: Start FISCO BCOS nodes..."
bash nodes/127.0.0.1/start_all.sh
if [ $? -eq 0 ]; then
    echo "✅ FISCO BCOS nodes started successfully."
else
    echo "❌ Failed to start FISCO BCOS nodes!"
    exit 1
fi

# 拉取 Webase-Front 安装包到 FISCO 目录
echo "🌐 Step 6: Download Webase-Front..."
cd $FISCO_DIR
wget -q https://github.com/WeBankBlockchain/WeBASELargeFiles/releases/download/v1.5.5/webase-front.zip &
download_pid=$!
show_loading $download_pid "⏳ Downloading Webase-Front package..."

# 等待下载完成并检查结果
wait $download_pid
if [ $? -eq 0 ]; then
    echo "✅ Webase-Front package downloaded."
else
    echo "❌ Failed to download Webase-Front package!"
    exit 1
fi

# 解压 Webase-Front 安装包
echo "📦 Step 7: Unzip Webase-Front..."
unzip webase-front.zip &
unzip_pid=$!
show_loading $unzip_pid "⏳ Unzipping Webase-Front package..."

# 等待解压完成并检查结果
wait $unzip_pid
if [ $? -ne 0 ]; then
    echo "❌ Unzip failed!"
    exit 1
else
    echo "✅ Webase-Front unzipped successfully."
fi

# 拷贝 FISCO BCOS 证书文件到 Webase 配置目录
echo "🔑 Step 8: Copy FISCO BCOS certificates..."
cd $FISCO_DIR/webase-front
cp -r $FISCO_DIR/nodes/127.0.0.1/sdk/* ./conf/
if [ $? -eq 0 ]; then
    echo "✅ Certificates copied successfully."
else
    echo "❌ Failed to copy certificates!"
    exit 1
fi

# 启动 Webase-Front 服务
echo "🚀 Step 9: Start Webase-Front..."
bash start.sh
if [ $? -eq 0 ]; then
    echo "✅ Webase-Front started successfully."
else
    echo "❌ Failed to start Webase-Front!"
    exit 1
fi

# 显示 Webase-Front 启动状态
echo "🔍 Step 10: Check Webase-Front status..."
bash status.sh
if [ $? -eq 0 ]; then
    echo "✅ Webase-Front is running."
else
    echo "❌ Webase-Front is not running!"
    exit 1
fi

# 完成，提示用户访问 Webase-Front
echo "🎉 Webase-Front is running. You can access it at: http://localhost:5002/WeBASE-Front"